package org.water.garden.activity.provider;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Im.Provider;
import android.view.Menu;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * @author sshwsfc@gmail.com
 * Display All im Providers for select
 */
public class ProviderList extends ListActivity {

	// Menu item Ids
	public static final int DELETE_ID = Menu.FIRST;
	public static final int INSERT_ID = Menu.FIRST + 1;
	
	private Cursor cursor;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);

		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Provider.CONTENT_URI);
		}
		
		cursor = Provider.query(getContentResolver(), new String[]{
			Provider._ID, Provider.NAME, Provider.FULLNAME
		});
		
		ListAdapter adapter = new SimpleCursorAdapter(this, 
				android.R.layout.simple_list_item_2, cursor, new String[] { 
					Provider.NAME, Provider.FULLNAME
				}, new int[] { android.R.id.text1, android.R.id.text2 });
		
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		cursor.moveTo(position);
		Uri url = ContentUris.withAppendedId(Provider.CONTENT_URI, id);
		url = Uri.withAppendedPath(url, cursor.getString(1));
		
		String action = getIntent().getAction();
		if (Intent.PICK_ACTION.equals(action) || Intent.GET_CONTENT_ACTION.equals(action)) {
			setResult(RESULT_OK, url.toString());
			finish();
		} else {
			
		}
	}
	
}
